//The contents of this file are subject to the Mozilla Public License Version 1.1
//(the "License"); you may not use this file except in compliance with the
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
//
//Software distributed under the License is distributed on an "AS IS" basis,
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
//for the specific language governing rights and
//limitations under the License.
//
//The Original Code is "The Columba Project"
//
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
//
//All Rights Reserved.
package org.columba.mail.gui.composer.command;

import java.io.InputStream;
import java.util.List;

import org.columba.api.command.ICommandReference;
import org.columba.api.command.IWorkerStatusController;
import org.columba.core.command.Command;
import org.columba.core.command.ProgressObservedInputStream;
import org.columba.core.command.Worker;
import org.columba.mail.command.ComposerCommandReference;
import org.columba.mail.composer.MessageBuilderHelper;
import org.columba.mail.composer.MessageComposer;
import org.columba.mail.composer.SendableMessage;
import org.columba.mail.folder.IMailbox;
import org.columba.mail.gui.composer.ComposerController;
import org.columba.mail.gui.composer.ComposerModel;
import org.columba.mail.util.MailResourceLoader;
import org.columba.ristretto.message.Address;
import org.columba.ristretto.parser.AddressParser;
import org.columba.ristretto.parser.ParserException;

/**
 * @author freddy
 */
public class SaveMessageCommand extends Command {
	private IMailbox folder;

	/**
	 * Constructor for SaveMessageCommand.
	 * 
	 * @param reference
	 */
	public SaveMessageCommand(ICommandReference reference) {
		super(reference);
	}

	/**
	 * @param worker
	 * @throws Exception
	 * @see org.columba.api.command.Command#execute(Worker)
	 */
	public void execute(IWorkerStatusController worker) throws Exception {
		ComposerCommandReference r = (ComposerCommandReference) getReference();

		ComposerController composerController = r.getComposerController();

		SendableMessage message = (SendableMessage) r.getMessage();

		if (message == null) {
			message = new MessageComposer(((ComposerModel) composerController
					.getModel())).compose(worker, r.isAppendSignature());
		}

		folder = (IMailbox) r.getSourceFolder();

		worker.setDisplayText(MailResourceLoader.getString("statusbar",
				"message", "save_message"));

		InputStream sourceStream = new ProgressObservedInputStream(message
				.getSourceStream(), worker);
		folder.addMessage(sourceStream, message.getHeader().getAttributes(),
				message.getHeader().getFlags());
		sourceStream.close();

		// Add all recipients to the collected addresses
		List recipients = message.getRecipients();
		if (recipients != null && recipients.size() > 0) {
			Address[] addresses = new Address[recipients.size()];
			for (int i = 0; i < recipients.size(); i++) {
				try {
					addresses[i] = AddressParser
							.parseAddress((String) recipients.get(i));
				} catch (ParserException e) {
					addresses[i] = addresses[i - 1];
				}
			}

			MessageBuilderHelper.addAddressesToAddressbook(addresses);
		}
	}
}